﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iOak.Domain.ReportDomain;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Business.Users;
using System.Reflection;

namespace iOak.Business.Report
{
    public class SummeryReportService
    {
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly CompanyService itemCompanyService = new CompanyService();
        private static readonly UserService itemUserService = new UserService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        public List<SummeryDomain> GetHeaderItem(Guid campaignId, Guid companyId)
        {
            var returnList = new List<SummeryDomain>();
            var itemApplication = itemApplicationService.GetListByCampaignIdAndCompanyId(campaignId, companyId);
            if (itemApplication != null)
            {
                var itemSummeryDomain = new SummeryDomain();
                itemSummeryDomain.CreateTime = itemApplication.AppliedDate.ToString("yyyy-MM-dd");

                var itemCompany = itemCompanyService.Retrieve(itemApplication.CompanyId);
                itemSummeryDomain.CompanyName = itemCompany.CompanyName;
                itemSummeryDomain.ContactEmail = itemCompany.ContactEmail;
                itemSummeryDomain.ContactPhone = itemCompany.ContactPhone;
                itemSummeryDomain.ContactUser = itemCompany.ContactPerson;
                returnList.Add(itemSummeryDomain);

            }

            return returnList;
        }

        public List<SummeryDetial> GetDetialList(Guid campaignId, Guid companyId)
        {
            var returnList = new List<SummeryDetial>();
            var itemApplication = itemApplicationService.GetListByCampaignIdAndCompanyId(campaignId, companyId);
            int number = 1;
            if (itemApplication != null)
            {
                // 获取候选人列表
                var listApplicationCandidates = itemApplicationCandidateService.GetPassedListByApplicationId(itemApplication.ApplicationId);

                // 根据候选人列表获取报表的对象
                listApplicationCandidates.ForEach(item =>
                    {
                        // 获取设定的值列表
                        var listResumeDetail = itemResumeDetailService.GetListByResumeId(item.ResumeId);

                        // 创建实例
                        var addItem = new SummeryDetial();
                        addItem.Number = number++;
                        // 获取对象类型
                        Type itemType = addItem.GetType();

                        // 获取属性列表          
                        var itemProperties = itemType.GetProperties().ToList();

                        itemProperties.ForEach(itemPropertie =>
                            {
                                // 获取当前key的值
                                var itemResumeDetail = listResumeDetail.FirstOrDefault(m => m.Key.Equals(itemPropertie.Name));
                                if (itemResumeDetail != null)
                                {
                                    itemPropertie.SetValue(addItem, itemResumeDetail.ValueString, null);
                                }
                            });

                        returnList.Add(addItem);
                    });
            }

            return returnList;
        }
    }
}
